package com.zhht.htzx.ui;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_state.BR;
import com.nsgf.library.data.bean.UserDataBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.zhht.htzx.R;

public class BusinessHomeActivity extends BaseActivity {

    private BusinessHomeStateHolder mStates;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(BusinessHomeStateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_business_home, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set("工作台");
        mAccountRequester.getBusinessDetail();
        mAccountRequester.getUserDataResult().observe(this, new Observer<ResponseStatus<UserDataBean>>() {
            @Override
            public void onChanged(ResponseStatus<UserDataBean> responseStatus) {
                if (responseStatus.getResponseCode() == 0) {
                    mStates.businessData.set(responseStatus.getData());
                }
            }
        });
    }

    public static class BusinessHomeStateHolder extends StateHolder {

        public final State<UserDataBean> businessData = new State<>(new UserDataBean());
    }

    public class ClickProxy {

        public void openLoginType() {
            UserInfo.getInstance().clearUserInfo();
            onResume();
            startActivity(new Intent().setClass(BusinessHomeActivity.this, LoginTypeActivity.class));
        }

        public void scanQRCode() {
            Intent intent = new Intent(BusinessHomeActivity.this, QRCodeScannerActivity.class);
            startActivity(intent);
        }

        public void openOrder() {
            startActivity(new Intent(BusinessHomeActivity.this, OrderBusinessListActivity.class));
        }

        public void openGoods() {
            startActivity(new Intent(BusinessHomeActivity.this, GoodsBusinessListActivity.class));
        }

        public void openWithdrawal() {
            startActivity(new Intent(BusinessHomeActivity.this, WithdrawalActivity.class)
                    .putExtra("amountWithdrawal", mStates.businessData.get().getAmountWithdrawal()));
        }

        public void openVirtualCurrency(int type) {
            startActivity(new Intent(BusinessHomeActivity.this, VirtualCurrencyActivity.class)
                    .putExtra("type", type)
                    .putExtra("data", mStates.businessData.get()));
        }

    }
}
